This homework is to practice using the leaflet package to make an
interactive map. The data used to construct this map was downloaded from
my Bryton Ryder 420e, which plotted my recorded ride up Daxueshan
National Forest Park (appr. 2400m) in Taichung City, Taiwan.
Importing the dataset for manipulation
# Preparing data----
SM_ride <- readFitFile("Data/220827075812.fit")
SM_ride
## Fit File
## ├╴File created: 2022-08-26 23:58:12
## ├╴Device: bryton 1901
## └╴Number of data messages: 19711
Manipulating data
Constructing a dataframe
# Merging all messages into one dataframe----
SMride_allrecords <- records(SM_ride) %>%
bind_rows() %>%
arrange(timestamp()) %>%
drop_na()
## ##------ Sat Oct 22 22:49:23 2022 ------##
head(SMride_allrecords)
## # A tibble: 6 × 9
## timestamp position_lat posit…¹ speed dista…² heart…³ cadence altit…⁴
## <dttm> <dbl> <dbl> <dbl> <dbl> <int> <int> <dbl>
## 1 2022-08-27 00:01:08 24.2 121. 6.97 1084. 148 57 333.
## 2 2022-08-27 00:01:09 24.2 121. 7.07 1095. 148 57 333.
## 3 2022-08-27 00:01:10 24.2 121. 7.04 1103. 149 57 333.
## 4 2022-08-27 00:01:11 24.2 121. 6.97 1109. 149 57 333.
## 5 2022-08-27 00:01:12 24.2 121. 6.88 1116. 148 56 333.
## 6 2022-08-27 00:01:13 24.2 121. 6.86 1122. 148 55 333.
## # … with 1 more variable: temperature <int>, and abbreviated variable names
## # ¹position_long, ²distance, ³heart_rate, ⁴altitude
Modifying dataframe
# Plotting a line between pings using nextlat and nextlng, creating new variables and subsetting other variables ----
SMride_allrecords <- SMride_allrecords %>%
mutate(var = c(1:nrow(SMride_allrecords))) %>%
mutate(nextLat = lead(position_lat),
nextLng = lead(position_long)) %>%
filter(var < nrow(SMride_allrecords)) %>%
select(-var) %>%
mutate(speedRound = round(speed,1)) %>%
mutate(altitudeRound = round(altitude,2)) %>%
mutate(distanceRound = round(distance,1))
SMride_allrecords[c(1:6),c(10:14)]
## # A tibble: 6 × 5
## nextLat nextLng speedRound altitudeRound distanceRound
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 24.2 121. 7 333. 1084.
## 2 24.2 121. 7.1 333. 1095.
## 3 24.2 121. 7 333. 1103
## 4 24.2 121. 7 333. 1109.
## 5 24.2 121. 6.9 333. 1116.
## 6 24.2 121. 6.9 333. 1122
Getting Coordinates
# long and lat coordinates----
SMride_allcoords <- SMride_allrecords %>%
select(position_long, position_lat)
head(SMride_allcoords)
## # A tibble: 6 × 2
## position_long position_lat
## <dbl> <dbl>
## 1 121. 24.2
## 2 121. 24.2
## 3 121. 24.2
## 4 121. 24.2
## 5 121. 24.2
## 6 121. 24.2
Constructing interactive map
#interactive map----
SMride_allmap <- SMride_allcoords %>%
as.matrix() %>%
leaflet( ) %>%
addTiles() %>%
addPolylines( )
SMride_allmap
Beyond this part is extra-work done to have fun playing with the
data, and to construct a more visually interpretable and interactive
map.
Constructing an altitude Map with previous cycling
data
# Arrange Unique Speeds In Ascending order----
altIndex <- SMride_altrecords %>%
select(altitudeRound) %>%
unique() %>%
arrange()
altIndex <- altIndex %>%
mutate(rank = 1:nrow(altIndex))
head(altIndex)
## # A tibble: 6 × 2
## altitudeRound rank
## <dbl> <int>
## 1 333. 1
## 2 333. 2
## 3 334 3
## 4 334. 4
## 5 334. 5
## 6 335. 6
#Assign a color to each unique altitude (blue low, red high)----
gradientFunction <- colorRampPalette(c("#09387A",
"#126CB3",
"#E9B851",
"#E98D2B",
"#E5493A",
"#B23227"))
colorIndex_alt <- as.data.frame(gradientFunction(nrow(altIndex))) %>%
mutate(rank = 1:nrow(altIndex))
head(colorIndex_alt)
## gradientFunction(nrow(altIndex)) rank
## 1 #09387A 1
## 2 #09387A 2
## 3 #09387A 3
## 4 #09387A 4
## 5 #09387A 5
## 6 #09387A 6
#Preparing color dataframe
colnames(colorIndex_alt) <- c("color", "rank")
colorIndex_alt$color <- as.character(colorIndex_alt$color)
#Adding columns to into larger dataframe
SMride_altrecords <- SMride_altrecords %>%
left_join(altIndex, by = "altitudeRound") %>%
left_join(colorIndex_alt, by = "rank")
SMride_altrecords[c(1:6),c(10:16)]
## # A tibble: 6 × 7
## nextLat nextLng speedRound altitudeRound distanceRound rank color
## <dbl> <dbl> <dbl> <dbl> <dbl> <int> <chr>
## 1 24.2 121. 7 333. 1084. 1 #09387A
## 2 24.2 121. 7.1 333. 1095. 1 #09387A
## 3 24.2 121. 7 333. 1103 1 #09387A
## 4 24.2 121. 7 333. 1109. 1 #09387A
## 5 24.2 121. 6.9 333. 1116. 1 #09387A
## 6 24.2 121. 6.9 333. 1122 1 #09387A
#Making an Altitude map----
alt_map <- leaflet() %>%
addTiles()
for(i in 1:nrow(SMride_altrecords)) {
alt_map <- addPolylines(map = alt_map,
data = SMride_altrecords,
lng = as.numeric(SMride_altrecords[i, c('position_long', 'nextLng')]),
lat = as.numeric(SMride_altrecords[i, c('position_lat', 'nextLat')]),
color = as.character(SMride_altrecords[i, c('color')])
)
}
alt_map %>%
addLegend("bottomright",
colors = c("#09387A", "#126CB3", "#E9B851", "#E98D2B", "#E5493A","#B23227"),
labels = c('0-400m','401-800m','801-1200m', '1201-1600m', '1601-1800m', '1801-2400m'),
title = "Altitude",
opacity = 1)